package com.qyhl.module_activities.act.detail;


import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.widget.FrameLayout;

import androidx.core.content.ContextCompat;

import com.qyhl.module_activities.R;
import com.qyhl.module_activities.R2;
import com.qyhl.module_activities.utils.view.NestedScrollWebView;
import com.qyhl.webtv.basiclib.base.BaseFragment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;

/**
 * 内容详情和结果公布页面
 */
public class ActivityDetailsFragment extends BaseFragment {
    private String detail;

    private boolean isFullScreen = false;
    private View fullview;

    @BindView(R2.id.webview)
    NestedScrollWebView webview;

    public static ActivityDetailsFragment newInstance(String detail) {
        ActivityDetailsFragment fragment = new ActivityDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("detail", detail);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_fragment_activity_details, container, false);
    }

    @Override
    protected void create() {
        detail = getArguments().getString("detail");

    }

    @Override
    public void initImmersionBar() {
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {
        init();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    private void init() {
        if (!TextUtils.isEmpty(detail)) {
            WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setDefaultTextEncodingName("utf-8");
            settings.setDomStorageEnabled(true);
            //是否阻塞加载网络图片  协议http or https
            settings.setBlockNetworkImage(false);
            settings.setUseWideViewPort(false);
            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            settings.setSupportZoom(false);
            settings.setBuiltInZoomControls(false);
            String htmlString = analysisContent(detail);
            webview.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null);
            webview.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onShowCustomView(View view, CustomViewCallback callback) {
                    showCustomView(view);
                }

                @Override
                public void onHideCustomView() {
                    hideCustomView();
                }
            });
        }
    }

    private void hideCustomView() {
        if (isFullScreen) {
            //设置竖屏
            if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }

            FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
            decorView.removeView(fullview);
            //清除全屏
            int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                uiOptions = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            decorView.setSystemUiVisibility(uiOptions);
        }
        isFullScreen = false;
    }

    private void showCustomView(View view) {
        if (isFullScreen) {
            hideCustomView();
        } else {
            //设置横屏
            if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }

            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
            FrameLayout frameLayout = new FrameLayout(getActivity());
            frameLayout.setLayoutParams(layoutParams);
            frameLayout.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.black));
            view.setLayoutParams(layoutParams);
            frameLayout.addView(view);
            decorView.addView(frameLayout);

            fullview = frameLayout;
            //设置全屏
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            isFullScreen = true;
        }
    }

    private String analysisContent(String htmlString) {
        Document doc = Jsoup.parse(htmlString);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("style", "max-width:100% !important;max-height:auto !important");
        }

        Elements elementsV = doc.getElementsByTag("video");
        for (Element element : elementsV) {
            element.attr("style", "max-width:100% !important;max-height:auto !important");
        }
        return doc.toString();
    }
}
